package test.list;

import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public int val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode a = new TreeNode('A');
        TreeNode b = new TreeNode('B');
        TreeNode c = new TreeNode('C');
        TreeNode d = new TreeNode('D');
        TreeNode e = new TreeNode('E');
        TreeNode f = new TreeNode('F');
//        TreeNode g = new TreeNode('G');


        a.left = b;
        a.right = c;
        b.left = d;
        b.right = e;
        c.left = f;
//        d.left = g;

        return a;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    // 非递归后序遍历
    public List<Integer> postorderTraversal(TreeNode root) {
        //返回数组
        List<Integer> list = new ArrayList<>();
        //利用栈的特性逆序
        Stack<TreeNode> s = new Stack();
        //临时存储
        Queue<TreeNode> q = new ArrayDeque<>();
        //先放入根节点
        q.add(root);
        //初始状态为一直取right，标为 0
        int flag = 0;
        //n 为循环变量（节点）
        TreeNode n = q.peek();
        while(!q.isEmpty()){
            n = q.peek();
            if(n.left == null){
                flag = 0;
            }
            if(n.right == null){
                flag = 1;
            }
            if(flag == 0){
                q.add(n.right);
            }else{
                q.add(s.peek().left);
            }
        }
        while(s.isEmpty()){
            int num = s.pop().val;
            list.add(num);
        }
        return list;
    }

    public static int nodeSize;

    /**
     * 获取树中节点的个数：遍历思路
     */
    void size(TreeNode root) {
        if(root == null){
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    int size2(TreeNode root) {
        if(root == null){
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    void getLeafNodeCount1(TreeNode root) {
        if(root == null){
            return;
        }
        if(root.right == null && root.left == null){
            leafSize++;
            return;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return;
    }

    /*
     获取叶子节点的个数：子问题
     */
    int getLeafNodeCount2(TreeNode root) {
        if(root == null){
            return 0 ;
        }
        if(root.right == null && root.left == null){
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    /*
    获取第K层节点的个数
     */
    int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null){
            return 0;
        }
        if(k == 0){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    int getHeight(TreeNode root){
        if(root == null){
            return 0 ;
        }
        if(root.right == null && root.left == null){
            return 1;
        }
        return 1 + (getHeight(root.left)>getHeight(root.right)?getHeight(root.left):getHeight(root.right));
    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        if(find(root.left,val) != null){
            return find(root.left,val);
        }
        if(find(root.right,val) != null){
            return find(root.right,val);
        }
        return null;
    }

    //层序遍历
    void levelOrder(TreeNode root) {
        if(root == null){
            return;
        }
        Queue<TreeNode> q = new ArrayDeque();
        q.add(root);
        while(!q.isEmpty()){
            root = q.peek();
            if(root.left != null){
                q.add(root.left);
            }
            if(root.right != null){
                q.add(root.right);
            }
            System.out.print(q.poll().val + " ");
        }

     }


    int flag = 1;
    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        if(root != null && root.left == null && root.right == null){
            return true;
        }
        if(root == null){
            if(flag == 2){
                return false;
            }
            flag = 2;
            return true;
        }

        if(isCompleteTree(root.left) == true){
            return isCompleteTree(root.right);
        } else{
            return isCompleteTree(root.left) == true;
        }
    }


    public static void test(){
        BinaryTree Tree = new BinaryTree();
        TreeNode root = Tree.createTree();

        System.out.println("前序遍历");
        Tree.preOrder(root);
        System.out.println();

        System.out.println("中序遍历");
        Tree.inOrder(root);
        System.out.println();

        System.out.println("后序遍历");
        Tree.postOrder(root);
        System.out.println();

        System.out.println("获取树中节点的个数：遍历思路");
        nodeSize = 0;
        Tree.size(root);
        System.out.println(nodeSize);

        System.out.println("获取节点的个数：子问题的思路");
        System.out.println(Tree.size2(root));

        System.out.println("获取叶子节点的个数：遍历思路");
        leafSize = 0;
        Tree.getLeafNodeCount1(root);
        System.out.println(leafSize);

        System.out.println("获取叶子节点的个数：子问题");
        System.out.println(Tree.getLeafNodeCount2(root));

        System.out.println("获取第K层节点的个数");
        System.out.println(Tree.getKLevelNodeCount(root,3));

        System.out.println("获取二叉树的高度");
        System.out.println(Tree.getHeight(root));

        System.out.println("检测值为‘A’的元素是否存在");
        System.out.println(Tree.find(root,'A'));

        System.out.println("层序遍历");
        Tree.levelOrder(root);
        System.out.println();

        System.out.println("判断一棵树是不是完全二叉树");
        System.out.println(Tree.isCompleteTree(root));;

    }

    public static void main(String[] args) {

        test();
    }
}
